package com.stablesoft.gamestat;

import com.stablesoft.gamestat.R;
import com.stablesoft.gamestat.data.wot.LoadDataTask;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
	static final int FIRST_RUN_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        
        updateButtonState();
        
        findViewById(R.id.btnWot).setOnClickListener(mWotClick);

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        boolean firstRun = settings.getBoolean(SettingsActivity.FIRST_RUN_KEY, true);
        if (firstRun)
        	startActivityForResult(new Intent(this, FirstRunActivity.class), FIRST_RUN_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == FIRST_RUN_CODE) {
			if (data != null && data.getBooleanExtra("ok", false)) {
				SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean(SettingsActivity.FIRST_RUN_KEY, false);
				editor.commit();
			}
			
			updateButtonState();
        }
    	
    	super.onActivityResult(requestCode, resultCode, data);
    }

    private void updateButtonState() {
    	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        String wotLogin = settings.getString(SettingsActivity.WOT_LOGIN_KEY, null);
        String mwoLogin = settings.getString(SettingsActivity.MWO_LOGIN_KEY, null);
        
        boolean wotEnabled = (wotLogin != null && wotLogin.isEmpty() == false);
        boolean mwoEnabled = (mwoLogin != null && mwoLogin.isEmpty() == false);
        
        findViewById(R.id.btnWot).setEnabled(wotEnabled);
    	findViewById(R.id.btnWot).setClickable(wotEnabled);
    	
    	findViewById(R.id.btnMwo).setEnabled(mwoEnabled);
    	findViewById(R.id.btnMwo).setClickable(mwoEnabled);
    	
    	if(wotEnabled || mwoEnabled)
    		findViewById(R.id.txtNameError).setVisibility(View.GONE);
    	else
    		findViewById(R.id.txtNameError).setVisibility(View.VISIBLE);
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if (item.getItemId() == R.id.action_settings) {
    		startActivity(new Intent(this, SettingsActivity.class));
    	}
    	return super.onOptionsItemSelected(item);
    }

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		updateButtonState();		
	}
	
	@Override
	protected void onResume() {
	    super.onResume();
	    
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        settings.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
	    super.onPause();
	    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        settings.unregisterOnSharedPreferenceChangeListener(this);
	}
    
	private OnClickListener mWotClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			new LoadDataTask(getApplicationContext()).execute();
		}
	};
}
